package com.ruoyi.project.king.series.controller;

import com.alibaba.fastjson.JSON;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.king.series.domain.Series;

import com.ruoyi.project.king.series.mapper.SeriesMapper;
import com.ruoyi.project.king.series.service.SeriesService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.json.Json;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/product/series")
public class LineManageController extends BaseController {

    //产品系列路径
    String prefix="/cms/product/series";

    @Autowired
    private SeriesService seriesService;


/*跳转到LineManage.html界面*/
    @GetMapping("")
    public String series()
    {
        return prefix + "/LineManage";
    }


    /*查询产品系列*/
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Series series)
    {
        startPage();
       /* List<Series> list=new ArrayList<>();
        list.add(new Series(1,"1223","2233"));*/

      List<Series> list=seriesService.findMore(series);


        return getDataTable(list);
    }


    /*跳转到系列添加界面*/
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }
    /*添加产品系列*/
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult  save( Series series)
    {
        if (seriesService.checkSeriesNameUnique(series)){
           return error("新增角色'" + series.getSname() + "'失败，角色名称已存在");
        }
        System.out.println(series);
        return toAjax(seriesService.save(series));
    }


    /*跳转到系列修改界面*/
    @GetMapping("/edit/{sid}")
    public String edit(@PathVariable("sid") int  sid,ModelMap map)
    {
        map.put("series",seriesService.findSeriesBySid(sid));
        return prefix + "/edit";
    }

    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult  editSave( Series series)
    {
        if (seriesService.checkSeriesNameUnique(series)){
            return error("修改角色'" + series.getSname() + "'失败，角色名称已存在");
        }

        System.out.println(series);
        return toAjax(seriesService.updateSeries(series));
    }


    /*********************删除系列**********************/
    @RequiresPermissions("product:series:remove")
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        System.out.println(ids);
        try
        {
            return toAjax(seriesService.deleteSeriesBySid(Integer.parseInt(ids)));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }










    /**
     * 校验角色名称
     */
    @PostMapping("/checkSeriesNameUnique")
    @ResponseBody
    public boolean checkSeriesNameUnique(Series series)
    {
        return seriesService.checkSeriesNameUnique(series);
    }

}
